package com.sprouting.ops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.OpsSysConfig;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsSysConfigQo;
import com.sprouting.ops.service.OpsSysConfigService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.IdTool;
import com.sprouting.ops.utils.NetTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统配置(OpsSysConfig)(OPS_SYS_CONFIG) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-08-16 22:10:22
 */
@Slf4j
@RestController
@RequestMapping("opsSysConfig")
public class OpsSysConfigController {

    @Resource
    private OpsSysConfigService opsSysConfigService;

    @Value("${server.port}")
    private String port;

    /**
     * 获取 druid 的地址
     * @return
     */
    @GetMapping("/getDruidPath")
    public ResultJson getDruidPath(){
        String ip = NetTool.getLocalHostLANAddress();
        String path = "http://%s:%s/ops/druid/login.html";
        path = String.format(path, ip, port);
        return ResultJson.success(path);
    }

    /**
     * 分页查询系统配置
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(OpsSysConfigQo query){
        if (query == null){
            query = new OpsSysConfigQo();
        }
        Page page = query.getPage();
        return ResultJson.success(opsSysConfigService.getListPage(page, query));
    }

    /**
     * 删除系统配置
     * @param id 系统配置id
     * @return
     */
    @PostMapping("delOpsSysConfig")
    @IdempotentSingle
    public ResultJson delOpsSysConfig(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        opsSysConfigService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 更新系统配置
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsSysConfig")
    @IdempotentSingle
    public ResultJson updOpsSysConfig(@Validated(UpdGroup.class) OpsSysConfig bo){
        String msg = checkOpsSysConfig(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        opsSysConfigService.updateById(bo);
        return ResultJson.success(bo);
    }

    /**
     * 保存系统配置
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsSysConfig")
    @IdempotentSingle
    public ResultJson saveOpsSysConfig(@Validated(AddGroup.class) OpsSysConfig bo){
        String msg = checkOpsSysConfig(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        bo.setId(IdTool.getId());
        opsSysConfigService.save(bo);
        return ResultJson.success(bo);
    }


    /**
     * 系统配置数据校验
     * @param bo 系统配置 校验数据
     * @return 非null则有错误
     */
    private String checkOpsSysConfig(OpsSysConfig bo){
        OpsSysConfig db = opsSysConfigService.getByCodeFilterId(bo.getId(), bo.getCode());
        if (db != null){
            return "系统代码已注册，请勿重复注册";
        }
        return null;
    }

}
